package com.starblink.product.detail.dialog

import android.app.Activity
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.product.databinding.DialogProductInfoBinding
import com.starblink.product.databinding.ItemProductCheckoutBinding
import com.starblink.rocketreserver.fragment.ProductD

class ProductInfoDialog(
    val context: Activity,
    val deliveryTime: ProductD.DeliveryTime?,
    val paymentWay: List<String>?,
    val morePayFlag: Boolean,
    val call: () -> Unit,
) : BaseBottomSheetFragment() {
    lateinit var mBinding: DialogProductInfoBinding
    override fun getBindingRootView(): View {
        mBinding = DialogProductInfoBinding.inflate(layoutInflater)
        return mBinding.root
    }

    lateinit var data: MutableList<String>

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(data).apply {
            addVhDelegate {
                ProductSecureHolder(
                    ItemProductCheckoutBinding.inflate(it.inflater, it, false)
                ) {
                    call.invoke()
                }
            }
        }
    }

    override fun initView() {
        var peekHeight = 58
        if (deliveryTime != null) {
            peekHeight += 76
        }
        Log.e("paymentWay","========$paymentWay")
        if (paymentWay?.isNotEmpty() == true) {
            data = paymentWay.toMutableList()
            if (morePayFlag) {
                data.add("More")
            }
            if (deliveryTime != null) {
                peekHeight += 36
            }
            peekHeight += 36
            peekHeight += if (data.size > 8) 144 else if (data.size > 4) 96 else 48
        }
        setPeekHeight(peekHeight.dp2px())
        if (deliveryTime != null) {
            mBinding.tvInfo.visibleOrGone(true) {
                mBinding.tvInfoTip.visibleOrGone(true) {}
            }

            val minDays = if (deliveryTime == null) 0 else deliveryTime.minDays
            val maxDays = if (deliveryTime == null) 0 else deliveryTime.maxDays
            if (minDays == maxDays) {
                mBinding.tvInfo.text = "Delivered within $minDays days"
            } else {
                mBinding.tvInfo.text = "Delivered within $minDays - $maxDays days"
            }
        }
        if (paymentWay?.isNotEmpty() == true) {

            mBinding.recycler.layoutManager =
                GridLayoutManager(mContext, 4)
            mBinding.recycler.addItemDecoration(
                SimpleGapItemDecor(horGap = 8.dp2px(), verGap = 8.dp2px(), withEdge = true)
            )
            mBinding.recycler.adapter = adapter
            mBinding.recycler.visibleOrGone(true) {
                mBinding.tvSecureTip.visibleOrGone(true) {}
            }
            if (deliveryTime != null) {
                mBinding.divide.visibleOrGone(true) {}
            }
        }

        mBinding.close.setOnClickListener {
            dismiss()
        }
    }

}